package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName UserServlet
 * @Description TODO
 * @Author CQ
 * @Date 2023/1/30 9:28
 * @Version 1.0
 **/
@WebServlet("/userservlet")
public class UserServlet extends BaseServlet{

    ApplicationContext ac =
            new ClassPathXmlApplicationContext("spring-context.xml");

    //<form action="userservlet?method=userLogin" method="post">
    public String userLogin (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1 获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)) {
            request.setAttribute("msg","用户名不能为空");
            return "/login.jsp";
        }
        if (StringUtils.isEmpty(password)) {
            request.setAttribute("msg","密码不能为空");
            return "/login.jsp";
        }
        // 2 调用业务逻辑
        try {
            UserService userService = ac.getBean(UserService.class);
            User user = userService.login(username,password);
            System.out.println(user);
            // 重定向
            request.getSession().setAttribute("user",user);
            return "redirect:/profileservlet?method=listProfile&pageNum=1";
        } catch (Exception e) {
            e.printStackTrace();
            // 转发
            request.setAttribute("msg","登录失败：" + e.getMessage());
            return "/login.jsp";
        }
    }

    // userservlet?method=userCheck&username="+username+"&password="+password
    public String userCheck (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)) {
            response.getWriter().write("1");
            return null;
        }
        if (StringUtils.isEmpty(password)) {
            response.getWriter().write("2");
            return null;
        }
        return null;
    }

    // <form action="userservlet?method=userRegist" method="post">
    public String userRegist (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取前端参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)) {
            request.setAttribute("msg","用户名不能为空");
            return "/regist.jsp";
        }
        if (StringUtils.isEmpty(password)) {
            request.setAttribute("msg","用户名不能为空");
            return "/regist.jsp";
        }
        // 调用业务逻辑
        try {
            User user = new User(username,password,1);
            UserService userService = ac.getBean(UserService.class);
            userService.register(user);
            // 重定向
            return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","注册失败：" + e.getMessage());
            // 转发
            return "/regist.jsp";
        }
    }
}
